﻿using System;
using System.Windows.Forms;
using CSLauncher.Hotkey.Interface;

namespace CSLauncher
{
    public partial class PreferencesDialog : Form
    {
        private static readonly object lockObj = new object();
        private static PreferencesDialog instance;

        public static void ShowSingle()
        {
            lock (lockObj)
            {
                if (instance != null)
                {
                    return;
                }

                instance = new PreferencesDialog();
            }

            instance.ShowDialog();
            instance.Dispose();
            instance = null;
        }
        
        private PreferencesDialog()
        {
            InitializeComponent();
        }

        private void PreferencesDialogLoad(object sender, EventArgs e)
        {
            foreach (Keys key in Enum.GetValues(typeof (Keys)))
            {
                keysBox.Items.Add(key);
            }

            HotkeyData data = Program.HotkeyManager[0];
            altBox.Checked = data.Alt;
            ctrlBox.Checked = data.Ctrl;
            shiftBox.Checked = data.Shift;
            winkeyBox.Checked = data.Winkey;
            keysBox.SelectedItem = data.Key;

            opacityTrackBar.Value = (int)(Properties.Settings.Default.MenuOpacity * 20);
        }

        private void OkButtonClick(object sender, EventArgs e)
        {
            HotkeyData currentHotkey = Program.HotkeyManager[0];
            HotkeyData newHotkey = new HotkeyData(altBox.Checked, shiftBox.Checked, ctrlBox.Checked, winkeyBox.Checked, (Keys)keysBox.SelectedItem);
            if (!newHotkey.Equals(currentHotkey))
            {
                Program.ChangeHotkey(newHotkey);
                Properties.Settings.Default.Hotkey = newHotkey;
            }

            Properties.Settings.Default.MenuOpacity = (double)opacityTrackBar.Value / 20;
            
            Properties.Settings.Default.Save();
            Close();
        }

        private void CancelButtonClick(object sender, EventArgs e)
        {
            Close();
        }
    }
}
